package com.wskj.web.controller.workbench;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.workbench.bo.web.TbUserJoinOrderAddBo;
import com.wskj.workbench.bo.web.TbUserJoinOrderEditBo;
import com.wskj.workbench.bo.web.TbUserJoinOrderQueryBo;
import com.wskj.workbench.service.ITbUserJoinOrderService;
import com.wskj.workbench.vo.web.TbUserJoinOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户入驻申请订单Controller
 *
 * @author wskj
 * @date 2024-09-25
 */
@Api(value = "用户入驻申请订单控制器", tags = {"用户入驻申请订单管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/workbench/userJoinorder")
public class TbUserJoinOrderController extends BaseController {

    private final ITbUserJoinOrderService iTbUserJoinOrderService;

    /**
     * 查询用户入驻申请订单列表
     */
    @ApiOperation("查询用户入驻申请订单列表")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:list')")
    @GetMapping("/list")
    public TableDataInfo<TbUserJoinOrderVo> list(@Validated TbUserJoinOrderQueryBo bo) {
        return iTbUserJoinOrderService.queryPageList(bo);
    }

    /**
     * 导出用户入驻申请订单列表
     */
    @ApiOperation("导出用户入驻申请订单列表")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:export')")
    @Log(title = "用户入驻申请订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated TbUserJoinOrderQueryBo bo) {
        List<TbUserJoinOrderVo> list = iTbUserJoinOrderService.queryList(bo);
        ExcelUtil<TbUserJoinOrderVo> util = new ExcelUtil<>(TbUserJoinOrderVo.class);
        return util.exportExcel(list, "用户入驻申请订单");
    }

    /**
     * 获取用户入驻申请订单详细信息
     */
    @ApiOperation("获取用户入驻申请订单详细信息")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:query')")
    @GetMapping("/{id}")
    public AjaxResult<TbUserJoinOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable("id") Long id) {
        return AjaxResult.success(iTbUserJoinOrderService.queryById(id));
    }

    /**
     * 新增用户入驻申请订单
     */
    @ApiOperation("新增用户入驻申请订单")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:add')")
    @Log(title = "用户入驻申请订单", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody TbUserJoinOrderAddBo bo) {
        return toAjax(iTbUserJoinOrderService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户入驻申请订单
     */
    @ApiOperation("修改用户入驻申请订单")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:edit')")
    @Log(title = "用户入驻申请订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody TbUserJoinOrderEditBo bo) {
        return toAjax(iTbUserJoinOrderService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户入驻申请订单
     */
    @ApiOperation("删除用户入驻申请订单")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:remove')")
    @Log(title = "用户入驻申请订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ids) {
        return toAjax(iTbUserJoinOrderService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入用户入驻申请订单数据
     */
    @ApiOperation("导入用户入驻申请订单数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('workbench:userJoinorder:import')")
    @Log(title = "用户入驻申请订单", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<TbUserJoinOrderVo> util = new ExcelUtil<>(TbUserJoinOrderVo.class);
        List<TbUserJoinOrderVo> tbUserJoinOrderList;
        try {
            tbUserJoinOrderList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iTbUserJoinOrderService.importTbUserJoinOrder(tbUserJoinOrderList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TbUserJoinOrderVo> util = new ExcelUtil<>(TbUserJoinOrderVo.class);
        return util.importTemplateExcel("用户入驻申请订单数据");
    }


    /**
     * 审核入驻申请
     */
    @ApiOperation("审核入驻申请")
    @RepeatSubmit
    @PostMapping("/audit")
    public AjaxResult<Void> audit(@Validated @RequestBody TbUserJoinOrderEditBo bo) {
        return toAjax(iTbUserJoinOrderService.audit(bo));
    }

}
